package com.atguigu.b_list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Demo1 {
    public static void main(String[] args) {
        //创建List集合
        List<String> list = new ArrayList<>();
        System.out.println("list = " + list);//[]

        //添加元素 -> 依次添加
        list.add("张三丰");
        list.add("张翠山");
        list.add("张无忌");

        //插入添加
        list.add(1,"灭绝师太");

        System.out.println("list = " + list);

        //五种遍历方式完成集合遍历
        //转数组
        Object[] objs = list.toArray();
        for (int i = 0; i < objs.length; i++) {
            String name = (String) objs[i];
            System.out.println("name = " + name);
        }
        System.out.println("---------------------------");
        //迭代器
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String name = it.next();
            System.out.println("name = " + name);
        }
        System.out.println("---------------------------");
        //增强for
        for (String name : list) {
            System.out.println("name = " + name);
        }
        System.out.println("---------------------------");
        //普通for
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i);
            System.out.println("name = " + name);
        }
        System.out.println("---------------------------");
        //列表迭代器
        ListIterator<String> lit = list.listIterator();
        while(lit.hasNext()){
            //获取下一个元素
            String name = lit.next();
            System.out.println("name = " + name);
        }
    }
}
